package com.lulin.mall.cart.controller;

import com.lulin.common.vo.MemberVo;
import com.lulin.mall.cart.interceptor.AuthInterceptor;
import com.lulin.mall.cart.service.ICartService;
import com.lulin.mall.cart.vo.Cart;
import com.lulin.mall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author lulin
 * @date 2022/12/14 14:39
 */
@Controller
public class CartController {

    @Autowired
    ICartService cartService;

    /**
     * 获取当前登录用户，购物车中的商品信息
     */
    @GetMapping("/getUserCartItems")
    @ResponseBody
    public List<CartItem> getUserCartItems() {
        return cartService.getUserCartItems();
    }

    /**
     * 购物车列表结算
     */
    @GetMapping("/cartList")
    public String getCartList(Model model) {
        Cart cart = cartService.getCartList();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 添加购物车
     *
     * @param skuId 商品sku编号
     * @param num   商品数量
     * @param model
     */
    @GetMapping("/addCart")
    public String addCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, Model model) {
        //把商品添加到购物车
        CartItem item = null;
        try {
            item = cartService.addCart(skuId, num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //往前台传数据，可以传对象，可以传List，通过el表达式 ${}可以获取到
        model.addAttribute("item", item);

        return "success";
    }

}
